package totpUtils;


import totpUtils.config.MTokenConstants;

import static constant.AppConstants.TOTP_STEP;

class CountUtils {

    private final static long mTimeStep = TOTP_STEP;
    private final static long mStartTime = 0;

    public static long millisToSeconds(long timeMillis) {
        return timeMillis / 1000;
    }

    /**
     * Gets the number of milliseconds since epoch.
     */
    public static long currentTimeMillis() {
        return System.currentTimeMillis() + SPUtils.getLong(MTokenConstants.PREFS_SERVICE_TIME_REDUCE, 0L);
    }

    public static long getValueAtTime(long time) {
        long timeSinceStartTime = time - mStartTime;
        if (timeSinceStartTime >= 0) {
            return timeSinceStartTime / mTimeStep;
        } else {
            return (timeSinceStartTime - (mTimeStep - 1)) / mTimeStep;
        }
    }

    public static long getValueStartTime(long value) {
        return mStartTime + (value * mTimeStep);
    }
}
